package com.ken.blog.tools;

import com.alibaba.fastjson.JSON;

import java.util.HashMap;
import java.util.Map;

/**
 * 封装输出类，用于统一输出返回json结果，方便app端取值和处理
 * 返回数据data为泛型，支持任意数据
 *
 * @param <T>
 */
public class Output<T> {
    private int code = 0;
    private String message = "获取数据成功";
    private T data;
    private String uri = "";

    public Output() {
        //直接实例化，不做任何处理
    }

    public Output(String uri) {
        this.uri = uri;
    }

    public Output(String uri, String message) {
        this.uri = uri;
        this.message = message;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public void setData(T data) {
        this.data = data;
    }

    public void setUri(String uri) {
        this.uri = uri;
    }

    /**
     * 输出json数据格式
     *
     * @return json
     */
    public Map json() {
        Map map = new HashMap();
        map.put("code", this.code);
        map.put("message", this.message);
        map.put("data", this.data);
        map.put("uri", this.uri);
        return map;
    }

    /**
     * 输出json字符串
     *
     * @return String
     */
    public String jsonString() {
        return JSON.toJSONString(this.json());
    }
}
